Read in the appropirate libraries
library(readr)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(xts)
## Warning: package 'xts' was built under R version 3.4.1
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.4.1
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:lubridate':
##
## intersect, setdiff, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.4.1
##
## Attaching package: 'leaflet'
## The following object is masked from 'package:xts':
##
## addLegend
library(DT)
## Warning: package 'DT' was built under R version 3.4.1
library(maps)
library(maptools)
## Loading required package: sp
## Checking rgeos availability: FALSE
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(viridisLite)
library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(treemap)
## Warning: package 'treemap' was built under R version 3.4.1
library(viridisLite)
read in the data and take a look under the hood.
database <- read_csv("C:/Users/John/projects/Mapping/earthquake-database/database.csv")
## Parsed with column specification:
## cols(
## .default = col_double(),
## Date = col_character(),
## Time = col_time(format = ""),
## Type = col_character(),
## `Depth Seismic Stations` = col_integer(),
## `Magnitude Type` = col_character(),
## `Magnitude Seismic Stations` = col_integer(),
## `Azimuthal Gap` = col_integer(),
## ID = col_character(),
## Source = col_character(),
## `Location Source` = col_character(),
## `Magnitude Source` = col_character(),
## Status = col_character()
## )
## See spec(...) for full column specifications.
## Warning in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 5041 parsing failures.
## row # A tibble: 5 x 5 col row col expected actual expected <int> <chr> <chr> <chr> actual 1 1848 Azimuthal Gap no trailing characters .4 file 2 1849 Azimuthal Gap no trailing characters .5 row 3 3379 Time time like 1975-02-23T02:58:41.000Z col 4 5980 Azimuthal Gap no trailing characters .2 expected 5 7513 Time time like 1985-04-28T02:53:41.530Z actual # ... with 1 more variables: file <chr>
## ... ................. ... ..................................................................... ........ ..................................................................... ...... ..................................................................... .... ..................................................................... ... ..................................................................... ... ..................................................................... ........ ..................................................................... ...... .......................................
## See problems(...) for more details.
head(database)
## # A tibble: 6 x 21
## Date Time Latitude Longitude Type Depth `Depth Error`
## <chr> <time> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 01/02/1965 13:44:18 19.246 145.616 Earthquake 131.6 NA
## 2 01/04/1965 11:29:49 1.863 127.352 Earthquake 80.0 NA
## 3 01/05/1965 18:05:58 -20.579 -173.972 Earthquake 20.0 NA
## 4 01/08/1965 18:49:43 -59.076 -23.557 Earthquake 15.0 NA
## 5 01/09/1965 13:32:50 11.938 126.427 Earthquake 15.0 NA
## 6 01/10/1965 13:36:32 -13.405 166.629 Earthquake 35.0 NA
## # ... with 14 more variables: `Depth Seismic Stations` <int>,
## # Magnitude <dbl>, `Magnitude Type` <chr>, `Magnitude Error` <dbl>,
## # `Magnitude Seismic Stations` <int>, `Azimuthal Gap` <int>, `Horizontal
## # Distance` <dbl>, `Horizontal Error` <dbl>, `Root Mean Square` <dbl>,
## # ID <chr>, Source <chr>, `Location Source` <chr>, `Magnitude
## # Source` <chr>, Status <chr>
summary(database)
## Date Time Latitude Longitude
## Length:23412 Length:23412 Min. :-77.080 Min. :-180.00
## Class :character Class1:hms 1st Qu.:-18.653 1st Qu.: -76.35
## Mode :character Class2:difftime Median : -3.568 Median : 103.98
## Mode :numeric Mean : 1.679 Mean : 39.64
## 3rd Qu.: 26.191 3rd Qu.: 145.03
## Max. : 86.005 Max. : 180.00
##
## Type Depth Depth Error
## Length:23412 Min. : -1.10 Min. : 0.000
## Class :character 1st Qu.: 14.52 1st Qu.: 1.800
## Mode :character Median : 33.00 Median : 3.500
## Mean : 70.77 Mean : 4.993
## 3rd Qu.: 54.00 3rd Qu.: 6.300
## Max. :700.00 Max. :91.295
## NA's :18951
## Depth Seismic Stations Magnitude Magnitude Type Magnitude Error
## Min. : 0.0 Min. :5.500 Length:23412 Min. :0.000
## 1st Qu.:146.0 1st Qu.:5.600 Class :character 1st Qu.:0.046
## Median :255.0 Median :5.700 Mode :character Median :0.059
## Mean :275.4 Mean :5.883 Mean :0.072
## 3rd Qu.:384.0 3rd Qu.:6.000 3rd Qu.:0.076
## Max. :934.0 Max. :9.100 Max. :0.410
## NA's :16315 NA's :23085
## Magnitude Seismic Stations Azimuthal Gap Horizontal Distance
## Min. : 0.00 Min. : 0.00 Min. : 0.005
## 1st Qu.: 10.00 1st Qu.: 22.00 1st Qu.: 0.969
## Median : 28.00 Median : 34.00 Median : 2.320
## Mean : 48.95 Mean : 45.26 Mean : 3.993
## 3rd Qu.: 66.00 3rd Qu.: 53.00 3rd Qu.: 4.724
## Max. :821.00 Max. :360.00 Max. :37.874
## NA's :20848 NA's :21151 NA's :21808
## Horizontal Error Root Mean Square ID Source
## Min. : 0.085 Min. :0.000 Length:23412 Length:23412
## 1st Qu.: 5.300 1st Qu.:0.900 Class :character Class :character
## Median : 6.700 Median :1.000 Mode :character Mode :character
## Mean : 7.663 Mean :1.023
## 3rd Qu.: 8.100 3rd Qu.:1.130
## Max. :99.000 Max. :3.440
## NA's :22256 NA's :6060
## Location Source Magnitude Source Status
## Length:23412 Length:23412 Length:23412
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
##
quake <- database
quake %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat=quake$Latitude, lng=quake$Longitude, clusterOptions = markerClusterOptions(),
popup= paste(quake$Type,
"<br><strong>Magnitude: </strong>", quake$Magnitude,
"<br><strong>Depth: </strong>", quake$Depth,
"<br><strong>Date: </strong>", quake$Date,
"<br><strong>Date: </strong>", quake$Time
))
quake %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lat=quake$Latitude, lng=quake$Longitude, weight=1, radius=1,
color= ifelse(quake$Magnitude>6.5,"red","yellow"),stroke=TRUE,
popup= paste(quake$Type,
"<br><strong>Magnitude: </strong>", quake$Magnitude,
"<br><strong>Depth: </strong>", quake$Depth,
"<br><strong>Date: </strong>", quake$Date,
"<br><strong>Date: </strong>", quake$Time)) %>%
addLegend(labels=c("Magnitude > 6.5", "Magnitude < 6.5"), colors=c("red","yellow"))
Preparing data - converting lat and lon to country names
world <- map('world', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(world$names, ":"), function(x) x[1])
world_sp <- map2SpatialPolygons(world, IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))
pointsSP <- SpatialPoints(cbind(x = quake$Longitude, y= quake$Latitude),
proj4string=CRS("+proj=longlat +datum=WGS84"))
indices <- over(pointsSP, world_sp)
stateNames <- sapply(world_sp@polygons, function(x) x@ID)
quake$Country <- stateNames[indices]
quake_country <- quake[!is.na(quake$Country),]
sum_country <- quake_country %>%
group_by(Country) %>%
summarise(Earthquakes=n())
sum_country %>%
hchart("treemap", hcaes(x = Country, value = Earthquakes, color=Earthquakes)) %>%
hc_credits(enabled = TRUE, style = list(fontSize = "10px")) %>%
hc_title(text = "Earthquakes per Country")